package org.dyndns.opendemogroup.optimizer;

import static org.junit.Assert.*;
import org.junit.Test;

/**
 * A class to test the class OptimizationProfile
 */
public class OptimizationProfileTest
{

	/**
	 * Tests the <i>fromXmlString</i> method with a typical input.
	 */
	@Test
	public void fromXmlString_Typical ( )
	{
		String input =
			"<java version=\"1.5.0\" class=\"java.beans.XMLDecoder\">"
					+ "<object class=\"org.dyndns.opendemogroup.optimizer.OptimizationProfile\">"
					+ "<void property=\"description\">"
					+ "<string>OneMax with 50 alleles, a population of 2000 and SlidingBias selection</string>"
					+ "</void>" + "</object>" + "</java>";
		OptimizationProfile actual = OptimizationProfile.fromXmlString ( input );
		assertEquals (
			"OneMax with 50 alleles, a population of 2000 and SlidingBias selection",
			actual.Description );
	}

	/**
	 * Convenience method which strips the initial processing instruction from a
	 * string representing XML.
	 * 
	 * @param input
	 *        A string representing XML.
	 * @return A string representing XML, without a processing instruction.
	 */
	private String stripProcessingInstruction ( String input )
	{
		// could be one of:
		// <?xml version="1.0" encoding="UTF-8"?>
		// <?xml version="1.0" encoding="UTF-8" standalone="no"?>

		// our unescaped regular expression:
		// \<\?xml[^<>]+?\?\>
		String result = input.replaceFirst ( "\\<\\?xml[^<>]+?\\?\\>", "" );
		return result;
	}

	/**
	 * Tests the <i>splitfromXmlString</i> method with an input that contains
	 * its own set of run seeds.
	 */
	@Test
	public void splitfromXmlString_WithExplicitRunSeeds ( )
	{
		String input =
			"<java version=\"1.5.0\" class=\"java.beans.XMLDecoder\">"
					+ "<object class=\"org.dyndns.opendemogroup.optimizer.OptimizationProfile\">"
					+ "<void property=\"maximumRuns\"><int>4</int></void>"
					+ "<void property=\"description\">"
					+ "<string>OneMax with 50 alleles, a population of 2000 and SlidingBias selection</string>"
					+ "</void>" + "<void property=\"runSeeds\">"
					+ "<array class=\"long\">" + "<long>42</long>"
					+ "<long>43</long>" + "<long>44</long>"
					+ "<long>20000000000</long>" + "</array>" + "</void>"
					+ "</object>" + "</java>";
		String[] actual = OptimizationProfile.splitFromXmlString ( input, 2 );
		assertEquals ( 2, actual.length );

		String one =
			"<java class=\"java.beans.XMLDecoder\" version=\"1.5.0\">"
					+ "<object class=\"org.dyndns.opendemogroup.optimizer.OptimizationProfile\">"
					+ "<void property=\"maximumRuns\"><int>2</int></void>"
					+ "<void property=\"description\">"
					+ "<string>OneMax with 50 alleles, a population of 2000 and SlidingBias selection</string>"
					+ "</void>" + "<void property=\"runSeeds\">"
					+ "<array class=\"long\">" + "<long>42</long>"
					+ "<long>43</long>" + "</array>" + "</void>" + "</object>"
					+ "</java>";
		assertEquals ( one, stripProcessingInstruction ( actual[0] ) );

		String two =
			"<java class=\"java.beans.XMLDecoder\" version=\"1.5.0\">"
					+ "<object class=\"org.dyndns.opendemogroup.optimizer.OptimizationProfile\">"
					+ "<void property=\"maximumRuns\"><int>2</int></void>"
					+ "<void property=\"description\">"
					+ "<string>OneMax with 50 alleles, a population of 2000 and SlidingBias selection</string>"
					+ "</void>" + "<void property=\"runSeeds\">"
					+ "<array class=\"long\">" + "<long>44</long>"
					+ "<long>20000000000</long>" + "</array>" + "</void>"
					+ "</object>" + "</java>";
		assertEquals ( two, stripProcessingInstruction ( actual[1] ) );
	}

	/**
	 * Tests the <i>splitfromXmlString</i> method with an input that does not
	 * contain its own set of run seeds and thus uses the defaults.
	 */
	@Test
	public void splitfromXmlString_WithDefaultRunSeeds ( )
	{
		String input =
			"<java version=\"1.5.0\" class=\"java.beans.XMLDecoder\">"
					+ "<object class=\"org.dyndns.opendemogroup.optimizer.OptimizationProfile\">"
					+ "<void property=\"maximumRuns\"><int>4</int></void>"
					+ "<void property=\"description\">"
					+ "<string>OneMax with 50 alleles, a population of 2000 and SlidingBias selection</string>"
					+ "</void>" + "</object>" + "</java>";
		String[] actual = OptimizationProfile.splitFromXmlString ( input, 2 );
		assertEquals ( 2, actual.length );

		String one =
			"<java class=\"java.beans.XMLDecoder\" version=\"1.5.0\">"
					+ "<object class=\"org.dyndns.opendemogroup.optimizer.OptimizationProfile\">"
					+ "<void property=\"maximumRuns\"><int>2</int></void>"
					+ "<void property=\"description\">"
					+ "<string>OneMax with 50 alleles, a population of 2000 and SlidingBias selection</string>"
					+ "</void>" + "<void property=\"runSeeds\">"
					+ "<array class=\"long\">" + "<long>0</long>"
					+ "<long>1</long>" + "</array>" + "</void>" + "</object>"
					+ "</java>";
		assertEquals ( one, stripProcessingInstruction ( actual[0] ) );

		String two =
			"<java class=\"java.beans.XMLDecoder\" version=\"1.5.0\">"
					+ "<object class=\"org.dyndns.opendemogroup.optimizer.OptimizationProfile\">"
					+ "<void property=\"maximumRuns\"><int>2</int></void>"
					+ "<void property=\"description\">"
					+ "<string>OneMax with 50 alleles, a population of 2000 and SlidingBias selection</string>"
					+ "</void>" + "<void property=\"runSeeds\">"
					+ "<array class=\"long\">" + "<long>2</long>"
					+ "<long>3</long>" + "</array>" + "</void>" + "</object>"
					+ "</java>";
		assertEquals ( two, stripProcessingInstruction ( actual[1] ) );
	}

}
